function GetXmlHttpObject()
{
	xmlHttp=null;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    		{
    			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    	}
  		catch (e)
    		{
	    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
  	}
	return xmlHttp;
};

function openFolder(b, foldername)
{
	b.add(1, foldername,'Node 1','javascript:void(0);');
};

function viewFolder(name)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	
	var url = "search.php";
		
//	var text = "<?xml version='1.0' encoding='ISO-8859-1'?>";
	var text;
	text = "<search>";
	text = text + "<dir>" + name + "</dir>";
	text = text + "</search>";
	
	xmlHttp.onreadystatechange = stateChanged; 
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Man", "GET search.php HTTP/1.1")
 	xmlHttp.setRequestHeader("MessageType", "CALL")
 	xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
	xmlHttp.send(text);
};

function stringHas(str, part)
{
	for(i=0; i<part.length; i++)
		if(str[i] != part[i])
			return false;
	return true;
};

var id=1;

function listfolder(name)
{
	id=id+1;
	elem = "<li id=\"" +name+ "\" noDrag=\"false\" noSiblings=\"false\" noDelete=\"false\" noRename=\"false\">" ;
	//elem = "<li id=\"" +name+ "\" noDrag=\"true\" noSiblings=\"true\" noDelete=\"true\" noRename=\"true\">" ;
	elem += "<a href=\"javascript:viewFolder(\'" + name + "\')\">" ;
	elem += name;
	elem += "</a><ul id=\"" +id+ "\" class=dhtmlgoodies_tree></ul></li>";

	return elem;
};

function listfile(name)
{
	elem = "<li id=\"" +name+ "\" noDrag=\"true\" noSiblings=\"true\" noDelete=\"true\" noRename=\"true\">" ;

	if(stringHas(name, "http://") || stringHas(name, "www") || stringHas(name, "file://"))
		elem += "<a href=\"" + name + "\">" ;
	else if(stringHas(name, "/") || stringHas(name, "C:"))
		elem += "<a href=\"file://" + name + "\">" ;
	else
		elem += "<a href=\"" + name + "\">" ;

	elem += name;
	elem += "</a><ul></ul></li>";
	return elem;
};

function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{// 4 = "loaded"
		xmlHttp.responseXML.preserveWhiteSpace = true;
		xmlDoc=xmlHttp.responseXML;
		root = xmlDoc.documentElement;

		document.getElementById('debug').innerHTML=xmlHttp.responseText;
		alert(xmlHttp.responseText);
		if( root.nodeName == "dirtree" )
                {
			var foldername;
			updatelem = document.getElementById("dirtree").getElementsByTagName('ul').item(0);

			updatelem.innerHTML = "";
                        for ( x=0; x<root.childNodes.length; x++ )
                        {
                                foldername = root.childNodes[x].childNodes[0].nodeValue;
                                updatelem.innerHTML += listfolder(foldername);
                        }
			//alert(updatelem.innerHTML);

                        return;
                }
		else if(root.nodeName == "files")
		{
			var filename;
			updatelem = document.getElementById("files")

			updatelem.innerHTML = "";
                        for( x=0; x<root.childNodes.length; x++ )
                        {
                                filename = root.childNodes[x].childNodes[0].nodeValue;
                                updatelem.innerHTML += listfile(filename);
                        }
			return;
		}

	}
	else if (xmlHttp.readyState=="OK")
	{
		alert("Problem retrieving data:" + xmlHttp.statusText);
	}
};
